package com.k12.user.controller;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.k12.user.model.entity.SysMenu;
import com.k12.user.service.SysMenuService;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
/**
 * 菜单权限表(SysMenu)表控制层
 *
 * @author makejava
 * @since 2025-07-28 20:53:09
 */
@RestController
@RequestMapping("sysMenu")
public class SysMenuController {
    /**
     * 服务对象
     */
    @Resource
    private SysMenuService sysMenuService;

    /**
     * 分页查询
     *
     * @param sysMenu 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping("getAllMenu")
    public List<SysMenu> queryByPage(SysMenu sysMenu) {
        return this.sysMenuService.queryByPage(sysMenu);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("getOneMenu/{id}")
    public SysMenu queryById(@PathVariable("id") Long id) {
        return this.sysMenuService.queryById(id);
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectMenuById")
    public SysMenu selectMenuById(@RequestParam("menuId")Long menuId,@RequestParam("tenantId") String tenantId) {
        return this.sysMenuService.selectMenuById(menuId,tenantId);
    }

    /**
     * 新增数据
     *
     * @param sysMenu 实体
     * @return 新增结果
     */
    @PostMapping("insertMenu")
    public SysMenu add(SysMenu sysMenu) {
        return this.sysMenuService.insert(sysMenu);
    }

    /**
     * 编辑数据
     *
     * @param sysMenu 实体
     * @return 编辑结果
     */
    @PostMapping("updateMenu")
    public SysMenu edit(SysMenu sysMenu) {
        return this.sysMenuService.update(sysMenu);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("deleteMenu")
    public Boolean deleteById(Long id) {
        return this.sysMenuService.deleteById(id);
    }


    @PostMapping("createPerm")
    public Integer createPerm(SysMenu sysMenu) {
        String menu_name = sysMenuService.getPermissionByTenantIdAndPerms(sysMenu.getTenantId(), sysMenu.getPerms());
        if(StringUtils.isNotBlank(menu_name)){
            return -1;
        }
        return sysMenuService.createPerm(sysMenu);
    }


    @GetMapping("selectMenuList")
    public List<SysMenu> selectMenuList(String tenantId) {
        return this.sysMenuService.selectMenuList(tenantId);
    }

    @GetMapping("selectMenuListByUserId")
    public List<SysMenu> selectMenuListByUserId(Long userId,String tenantId) {
        return this.sysMenuService.selectMenuListByUserId(userId,tenantId);
    }


    @GetMapping("selectMenuListByRoleId")
    public List<Long> selectMenuListByRoleId(@RequestParam("userId") Long userId,@RequestParam("b")Boolean b,@RequestParam("tenantId")String tenantId) {
        return this.sysMenuService.selectMenuListByRoleId(userId,b,tenantId);
    }


    @GetMapping("checkMenuNameUnique")
    public SysMenu checkMenuNameUnique(@RequestParam("menuName")String menuName,@RequestParam("tenantId") Long tenantId,@RequestParam("parentId") Long parentId) {
        return this.sysMenuService.checkMenuNameUnique(menuName,tenantId,parentId);
    }

    @GetMapping("hasChildByMenuId")
    public int hasChildByMenuId(@RequestParam("menuId")Long menuId,@RequestParam("tenantId") String tenantId) {
        return this.sysMenuService.hasChildByMenuId(menuId,tenantId);
    }

    @GetMapping("deleteMenuById")
    public int deleteMenuById(@RequestParam("menuId")Long menuId,@RequestParam("tenantId") String tenantId) {
        return this.sysMenuService.deleteMenuById(menuId,tenantId);
    }




}

